﻿using EasyDDD.AdminModule.Domain.DomainEventsSysMenu;
using EasyDDD.Domain;
using Microsoft.EntityFrameworkCore;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json.Serialization;

namespace EasyDDD.AdminModule.Domain.AggregateSysMenu
{
    public record SysMenuId(Guid Value) : Key<Guid>(Value);
    //[Table(nameof(SysMenu))]
    public partial class SysMenu : DomainEntity<SysMenuId>, IAggregateRoot
    {
        protected SysMenu() { }

        [JsonConstructor]
        public SysMenu(Guid parentId, byte menuType, string title, string name, string path, string component, int sort, string redirect, string icon, bool keepalive, bool hiddenTag, bool fixedTag, byte status, bool show, string permission, string extJson)
        {
            ParentId = parentId;
            MenuType = menuType;
            Title = title;
            Name = name;
            Path = path;
            Component = component;
            Sort = sort;
            Redirect = redirect;
            Icon = icon;
            Keepalive = keepalive;
            HiddenTag = hiddenTag;
            FixedTag = fixedTag;
            Status = status;
            Show = show;
            Permission = permission;
            ExtJson = extJson;
        }

        #region 属性

        //[ForeignKey(nameof(ParentId))]
        //public SysMenu? Parent { get; private set; }

        /// <summary>
        /// 父导航ID
        /// </summary>
        [Comment("父导航ID")]
        public Guid ParentId { get; private set; } = Guid.Empty;

        /// <summary>
        /// /////菜单类型（`0`代表菜单、`1`代表`iframe`、`2`代表外链、`3`代表按钮） 4.路由重定向
        /// 菜单类型 0.目录 1.菜单 2.按钮 3.重定向
        /// </summary>
        [Comment("菜单类型")]
        public byte MenuType { get; private set; } = 0;

        /// <summary>
        /// 菜单名称
        /// </summary>
        [Comment("菜单名称")]
        [StringLength(50)]
        [Required]
        public string Title { get; private set; } = string.Empty;

        /// <summary>
        /// 组件名称
        /// </summary>
        [Comment("组件名称")]
        [StringLength(50)]
        [Required]
        public string Name { get; private set; } = string.Empty;

        /// <summary>
        /// 路由地址
        /// </summary>
        [Comment("路由地址")]
        [StringLength(200)]
        public string Path { get; private set; } = string.Empty;

        /// <summary>
        /// 组件路径
        /// </summary>
        [Comment("组件路径")]
        [StringLength(200)]
        public string Component { get; private set; } = string.Empty;

        /// <summary>
        /// 排序
        /// </summary>
        [Comment("排序")]
        [Required]
        public int Sort { get; private set; } = 1000;

        /// <summary>
        /// 路由重定向
        /// </summary>
        [Comment("路由重定向")]
        [StringLength(512)]
        public string Redirect { get; private set; } = string.Empty;

        /// <summary>
        /// 菜单图标
        /// </summary>
        [Comment("菜单图标")]
        [StringLength(512)]
        public string Icon { get; private set; } = string.Empty;

        /// <summary>
        /// 是否缓存
        /// </summary>
        [Comment("是否缓存")]
        public bool Keepalive { get; private set; } = false;

        /// <summary>
        /// 显示标签页
        /// </summary>
        [Comment("显示标签页")]
        public bool HiddenTag { get; private set; } = false;

        /// <summary>
        /// 固定标签页
        /// </summary>
        [Comment("固定标签页")]
        public bool FixedTag { get; private set; } = false;

        /// <summary>
        /// 状态
        /// </summary>
        [Comment("状态")]
        [Required]
        public byte Status { get; private set; } = 0;

        /// <summary>
        /// 是否显示
        /// </summary>
        [Comment("是否显示")]
        public bool Show { get; private set; } = false;

        /// <summary>
        /// 权限标识
        /// </summary>
        [Comment("权限标识")]
        [StringLength(200)]
        public string Permission { get; private set; } = string.Empty;

        /// <summary>
        /// 扩展字段
        /// </summary>
        [Comment("扩展字段")]
        [Required(AllowEmptyStrings = true)]
        public string ExtJson { get; private set; } = string.Empty;
        #endregion

        public override void Add()
        {
            if (Id.Value != Guid.Empty)
            {
                throw new InvalidOperationException($"该实体{this.GetType().Name}:{this.Id}已创建！");
            }
        }

        public new void Delete()
        {
            IsDeleted = true;
            AddDomainEvent(new SysMenuDeletDomainEvent(this));
        }

        public void SetPermission(string permission)
        {
            if (Permission != permission)
            {
                Permission = permission ?? throw new ArgumentNullException(nameof(permission));
            }
            AddDomainEvent(new SysMenuSetPermissionDomainEvent(this));
        }
    }
}
